<?php
/**
 * Delegates the processing of an Order to various helper classes.
 * @copyright 2008 Nicolas Schudel
 * @copyright Portions Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */

class OrderFactory {
	private $orders;
	private $ordersproducts;
	
	function __construct() {
		$this->orders = ServiceLocator::getService('Orders');
		$this->ordersproducts = ServiceLocator::getService('OrdersProducts');
	}
	
	private function validate($dto) {
		foreach ($dto->products as $p) if (empty($p->pid)) throw new Exception("There are no product IDs in the Order");
		if (empty($dto->price)) throw new Exception("There are is no price defined");
		return true;
	}
	
	public function execute(OrderDTO $dto) {
		if (!$this->validate($dto)) throw new Exception("The DTO is not valid");
		$this->ordersproducts->load($dto);
		$this->orders->load($dto);
		$this->orders->save(null);
		$this->ordersproducts->save($this->orders->getInsertId());
		return true;
	}
	
	public function createService($service ) {}

	public function hasService() {}
}
?>
